package com.example.thymeleafrbac.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.thymeleafrbac.domain.JsonOV;
import com.example.thymeleafrbac.pojo.SysUser;
import com.example.thymeleafrbac.service.SysUserService;
import com.example.thymeleafrbac.util.MD5Util;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class SysUserController {

    @Resource
    private SysUserService service;

    String salt = "hello";//加入的盐


    @GetMapping("console1")
    public String console1(){
        return "view/console/console1";
    }
    @GetMapping("console2")
    public String console2(){
        return "view/console/console2";
    }

    @GetMapping("theme")
    public String theme(){
        return "view/system/theme";
    }

    @GetMapping("core")
    public String core(){
        return "view/document/core";
    }

    //用户管理
    @GetMapping("user")
    public String user(){
        return "view/system/user";
    }

    @GetMapping(value = "userJSON", produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String userJSON(Integer page,Integer limit,String loginName,String phonenumber,Integer status){
        IPage<SysUser> iPage=new Page<>(page,limit);
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        if (loginName!="" && null!=loginName) {
            wrapper.eq("login_name", loginName);
        }
        if (phonenumber!="" && null!=phonenumber) {
            wrapper.eq("phonenumber", phonenumber);
        }
        if (null!=status && status !=2) {
            wrapper.eq("status", status);
        }
        iPage = service.page(iPage,wrapper);
        int count = service.count();
        JsonOV jsonOV=new JsonOV(0,"",count,iPage.getRecords());
        return JSON.toJSONString(jsonOV);
    }
    @GetMapping("toAdd")
    public String toAdd(){
        return "view/system/operate/add";
    }
    
    
    @PostMapping("add")
    @ResponseBody
    public String add(SysUser sysUser){
        String newPassword= MD5Util.backToDb( sysUser.getPassword(),salt);
        sysUser.setPassword(newPassword);
        sysUser.setSalt(salt);
        //跟新时间
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        sysUser.setCreateTime(date);
        boolean save = service.save(sysUser);

        return String.valueOf(save);
    }

    @GetMapping("remove/{id}")
    @ResponseBody
    public String delete(@PathVariable Integer id){
        boolean b = service.removeById(id);
        return String.valueOf(b);
    }

    @GetMapping("select/{id}")
    public String selectId(@PathVariable Integer id, Model model){
        SysUser sysUser = service.getById(id);
        model.addAttribute("user",sysUser);
        return "view/system/operate/edit";
    }

    @PostMapping("select/update")
    @ResponseBody
    public String update(SysUser sysUser){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        String newPassword= MD5Util.backToDb( sysUser.getPassword(),salt);
        sysUser.setPassword(newPassword);
        sysUser.setSalt(salt);
        Date date = new Date(System.currentTimeMillis());
        sysUser.setPwdUpdateDate(date);
        sysUser.setPwdUpdateDate(date);

        boolean update = service.updateById(sysUser);

        return String.valueOf(update);
    }



    @GetMapping("status/{id}")
    @ResponseBody
    public String status(@PathVariable Integer id){
        SysUser byId = service.getById(id);
        boolean update=false;
        if (byId!=null){
            if (byId.getStatus().equals("0")){
                byId.setStatus("1");
            }else {
                byId.setStatus("0");
            }
             update = service.updateById(byId);

        }
        return String.valueOf(update);
    }

}
